package com.ruoyi.settled.service.impl;

import com.ruoyi.settled.domain.AgreementRule;
import com.ruoyi.settled.domain.SettlementAgreement;
import com.ruoyi.settled.mapper.SettlementAgreementMapper;
import com.ruoyi.settled.service.ISettlementAgreementService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 结算协议Service业务层处理
 * 
 * @author huangchen
 * @date 2025-01-18
 */
@Service
public class SettlementAgreementServiceImpl implements ISettlementAgreementService 
{
    @Autowired
    private SettlementAgreementMapper settlementAgreementMapper;

    /**
     * 查询结算协议
     * 
     * @param agreementCode 结算协议主键
     * @return 结算协议
     */
    @Override
    public SettlementAgreement selectSettlementAgreementByAgreementCode(String agreementCode)
    {
        return settlementAgreementMapper.selectSettlementAgreementByAgreementCode(agreementCode);
    }

    /**
     * 查询结算协议列表
     * 
     * @param settlementAgreement 结算协议
     * @return 结算协议
     */
    @Override
    public List<SettlementAgreement> selectSettlementAgreementList(SettlementAgreement settlementAgreement)
    {
        return settlementAgreementMapper.selectSettlementAgreementList(settlementAgreement);
    }

    /**
     * 新增结算协议
     * 
     * @param settlementAgreement 结算协议
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSettlementAgreement(SettlementAgreement settlementAgreement)
    {
        int rows = settlementAgreementMapper.insertSettlementAgreement(settlementAgreement);
        insertAgreementRule(settlementAgreement);
        return rows;
    }

    /**
     * 修改结算协议
     * 
     * @param settlementAgreement 结算协议
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSettlementAgreement(SettlementAgreement settlementAgreement)
    {
        settlementAgreementMapper.deleteAgreementRuleByAgreementCode(settlementAgreement.getAgreementCode());
        insertAgreementRule(settlementAgreement);
        return settlementAgreementMapper.updateSettlementAgreement(settlementAgreement);
    }

    /**
     * 批量删除结算协议
     * 
     * @param agreementCodes 需要删除的结算协议主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettlementAgreementByAgreementCodes(String[] agreementCodes)
    {
        settlementAgreementMapper.deleteAgreementRuleByAgreementCodes(agreementCodes);
        return settlementAgreementMapper.deleteSettlementAgreementByAgreementCodes(agreementCodes);
    }

    /**
     * 删除结算协议信息
     * 
     * @param agreementCode 结算协议主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSettlementAgreementByAgreementCode(String agreementCode)
    {
        settlementAgreementMapper.deleteAgreementRuleByAgreementCode(agreementCode);
        return settlementAgreementMapper.deleteSettlementAgreementByAgreementCode(agreementCode);
    }

    /**
     * 查询结算协议
     *
     * @param partnerCode 结算协议外键
     * @return 结算协议
     */
    @Override
    public SettlementAgreement selectSettlementAgreementByPartnerCode(String partnerCode)
    {
        return settlementAgreementMapper.selectSettlementAgreementByPartnerCode(partnerCode);
    }

    @Override
    public String selectSettlementAgreementCodeByPartnerCode(String partnerCode) {
        return settlementAgreementMapper.selectAgreementCodeByPartnerCode(partnerCode);
    }

    /**
     * 新增协议规则信息
     * 
     * @param settlementAgreement 结算协议对象
     */
    public void insertAgreementRule(SettlementAgreement settlementAgreement)
    {
        List<AgreementRule> agreementRuleList = settlementAgreement.getAgreementRuleList();
        String agreementCode = settlementAgreement.getAgreementCode();
        if (StringUtils.isNotNull(agreementRuleList))
        {
            List<AgreementRule> list = new ArrayList<AgreementRule>();
            for (AgreementRule agreementRule : agreementRuleList)
            {
                agreementRule.setAgreementCode(agreementCode);
                list.add(agreementRule);
            }
            if (list.size() > 0)
            {
                settlementAgreementMapper.batchAgreementRule(list);
            }
        }
    }
}
